class _root extends MovieClip
{
	var acceleration = 0.4;
	var speedDecay = 0.96;
	var rotationStep = 10;
	var maxSpeed = 10;
	var backSpeed = 1;

	var car1;
	var shadow1;
	var terrain;
	var stepper;

	function init() {
		terrain = attachMovie("Track", "track", _root.getNextHighestDepth());
		car1 = attachMovie("Car", "car1", _root.getNextHighestDepth());
		shadow1 = attachMovie("Shadow", "shadow1", _root.getNextHighestDepth());
		stepper = attachMovie("Stepper", "stepper", _root.getNextHighestDepth());

		car1.code = "player";				
	}
	
	function step(who) {
		who = 1;
		//check to see if the car in question is controlled by the player or by the computer
		if (_root["car"+who].code == "player") {
			//we will constantly decrease speed by multiplying it with a number below 1
			if (this["speed"+who]>0.3) {
				this["speed"+who] *= _root.speedDecay;
			} else {
				this["speed"+who] = 0;
			}
			//the car will react to certain keys
			//accelerate
			if (Key.isDown(Key.UP) && this["speed"+who]<_root.maxSpeed) {
				this["speed"+who] += _root.acceleration;
			}
			//brake (reverse)
			if (Key.isDown(Key.DOWN)) {
				this["speed"+who] -= _root.backSpeed;
			}
			//steer left
			if (Key.isDown(Key.LEFT) && Math.abs(this["speed"+who])>0.3) {
				_root["car"+who]._rotation -= _root.rotationStep*(this["speed"+who]/_root.maxSpeed);
			}
			//steer right
			if (Key.isDown(Key.RIGHT) && Math.abs(this["speed"+who])>0.3) {
				_root["car"+who]._rotation += _root.rotationStep*(this["speed"+who]/_root.maxSpeed);
			}
			this["rotation"+who] = _root["car"+who]._rotation;
			//we calculate the two components of speed (X axis and Y axis)
			this["speedx"+who] = Math.sin(this["rotation"+who]*(Math.PI/180))*this["speed"+who];
			this["speedy"+who] = Math.cos(this["rotation"+who]*(Math.PI/180))*this["speed"+who]*-1;
			//apply the components on the actual position of the car
			_root["car"+who]._x += this["speedx"+who];
			_root["car"+who]._y += this["speedy"+who];
			
			//the collisions
			//define the four collision points
			_root["car"+who].pointLeft = {x:-20, y:0};
			_root["car"+who].localToGlobal(_root["car"+who].pointLeft);
			_root["car"+who].pointRight = {x:20, y:0};
			_root["car"+who].localToGlobal(_root["car"+who].pointRight);
			_root["car"+who].pointFront = {x:0, y:-25};
			_root["car"+who].localToGlobal(_root["car"+who].pointFront);
			_root["car"+who].pointBack = {x:0, y:25};
			_root["car"+who].localToGlobal(_root["car"+who].pointBack);
			//let's use some shorter variable names :)
			this["lpx"+who] = _root["car"+who].pointLeft.x;
			this["lpy"+who] = _root["car"+who].pointLeft.y;
			this["rpx"+who] = _root["car"+who].pointRight.x;
			this["rpy"+who] = _root["car"+who].pointRight.y;
			this["fpx"+who] = _root["car"+who].pointFront.x;
			this["fpy"+who] = _root["car"+who].pointFront.y;
			this["bpx"+who] = _root["car"+who].pointBack.x;
			this["bpy"+who] = _root["car"+who].pointBack.y;
			
			//check for collisions
			if (_root.terrain.hitTest(this["lpx"+who], this["lpy"+who], true)){
				_root["car"+who]._rotation += 5;
				this["speed"+who] *= 0.85;
			}
			if (_root.terrain.hitTest(this["rpx"+who], this["rpy"+who], true)){
				_root["car"+who]._rotation -= 5;
				this["speed"+who] *= 0.85;
			}
			if (_root.terrain.hitTest(this["fpx"+who], this["fpy"+who], true)){
				this["speed"+who] = -1;
			}
			if (_root.terrain.hitTest(this["bpx"+who], this["bpy"+who], true)){
				this["speed"+who] = 1;
			}
					
			//position the shadow of the car
			_root["shadow"+who]._x = _root["car"+who]._x-4;
			_root["shadow"+who]._y = _root["car"+who]._y+2;
			_root["shadow"+who]._rotation = _root["car"+who]._rotation;
		}
		if (_root["car"+who].code == "computer") {
		}
	}
}